package com.ichart.doc;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.ichart.js.ScriptHelp;
import com.ichart.utils.FileUtils;

public class Comparison {
	public static ComponentResolver resolver = new ComponentResolver();
	public static List<String> listjs = ScriptHelp.getChartJs();
	public static int CONFLICT_PROPERTY_NUMBER = 0;
	public static int CONFLICT_METHOD_NUMBER = 0;
	public static int CONFLICT_EVENT_NUMBER = 0;
	
	public static List<Component> chinese(){
		Info.setLanguage(Info.CHINESE);
		Info.setSrcPath("E:/workspace8.6/ichartJava/src/js/");
		return bulid();
	}
	
	public static List<Component> english(){
		Info.setLanguage(Info.ENGLISH);
		Info.setSrcPath("E:/workspace8.6/ichartjs/src/");
		return bulid();
	}
	
	public static List<Component> bulid(){
		List<Component> All = new ArrayList<Component>();
		try {
			for (String string : listjs) {
				All.addAll(resolver.resolver(FileUtils.readFileToString(new File(Info.SrcPath +string))));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return All;
	}
	public static Component get(List<Component> All,Component c){
		for(Component e:All){
			if(e.getClassName().equals(c.getClassName())){
				return e;
			}
		}
		return null;
	}
	
	public static void start(List<Component> news,List<Component> old){
		for(Component e1:news){
			Component e2 = get(old,e1);
			if(e2==null){
				throw new NullPointerException("获取不到比对对象!");
			}
			property(e1,e2);
			method(e1,e2);
			event(e1,e2);
		}
		System.out.println("Conflict Property:"+CONFLICT_PROPERTY_NUMBER);
		System.out.println("Conflict Method:"+CONFLICT_METHOD_NUMBER);
		System.out.println("Conflict Event:"+CONFLICT_EVENT_NUMBER);
	}
	
	public static void event(Component e1,Component e2){
		boolean exist = false;
		List<String> log = new ArrayList<String>();
		
		for (Event it1 : e1.getEvents()) {
			for (Event it2 : e2.getEvents()) {
				if(it1.getMethod().equalsIgnoreCase(it2.getMethod())){
					exist = true;
					break;
				}
			}
			if(!exist){
				CONFLICT_EVENT_NUMBER++;
				log.add("##event["+it1.getMethod()+"] not exist,please synchronize!");
			}
			exist = false;
		}
		if(log.size()>0){
			System.out.println("======start event ["+e1.getClassName()+"]");
			System.out.println("Now:["+e1.getEvents().size()+"]");
			System.out.println("Old:["+e2.getEvents().size()+"]");
			for (String string : log) {
				System.out.println(string);
			}
			System.out.println("------end** event ["+e1.getClassName()+"]");
			System.out.println();
		}
	}
	
	public static void method(Component e1,Component e2){
		boolean exist = false;
		List<String> log = new ArrayList<String>();
		for (Method it1 : e1.getMethods()) {
			for (Method it2 : e2.getMethods()) {
				if(it1.getMethod().equalsIgnoreCase(it2.getMethod())){
					exist = true;
					break;
				}
			}
			if(!exist){
				CONFLICT_METHOD_NUMBER++;
				log.add("##method["+it1.getMethod()+"] not exist,please synchronize!");
			}
			exist = false;
		}
		if(log.size()>0){
			System.out.println("======start method ["+e1.getClassName()+"]");
			System.out.println("Now:["+e1.getMethods().size()+"]");
			System.out.println("Old:["+e2.getMethods().size()+"]");
			for (String string : log) {
				System.out.println(string);
			}
			System.out.println("------end** method ["+e1.getClassName()+"]");
			System.out.println();
		}
	}
	
	public static void property(Component e1,Component e2){
		boolean exist = false;
		List<String> log = new ArrayList<String>();
		for (Item it1 : e1.getItems()) {
			for (Item it2 : e2.getItems()) {
				if(it1.getAttribute().equalsIgnoreCase(it2.getAttribute())){
					exist = true;
					break;
				}
			}
			if(!exist){
				CONFLICT_PROPERTY_NUMBER++;
				log.add("##property["+it1.getAttribute()+"] not exist,please synchronize!");
			}
			exist = false;
		}
		if(log.size()>0){
			System.out.println("======start property ["+e1.getClassName()+"]");
			System.out.println("Now:["+e1.getItems().size()+"]");
			System.out.println("Old:["+e2.getItems().size()+"]");
			for (String string : log) {
				System.out.println(string);
			}
			System.out.println("------end** property ["+e1.getClassName()+"]");
			System.out.println();
		}
	}
	
	public static void main(String[] args) {
		List<Component> english = english();
		List<Component> chinese = chinese();
		start(english,chinese);
		System.out.println("================================");
		start(chinese,english);
		
		
	}
}
